package com.yzl.meeting.business.utils;

/**
 * @Title: SendJMail
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/11 21:15
 * @Version: 1.0
 */
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendJMail {
    private static Properties prop = new Properties();
    private static String emailUser ;
    private static String emailPwd;
    private static String emailHost;
    private static String emailAuth;
    private static String emailProtocol;
    private static int emailPort;
    static {
        //读取src下的资源
        InputStream input = SendJMail.class.getClassLoader().getResourceAsStream("email.properties");
        try {
            prop.load(input);
            emailUser = prop.getProperty("emailUser");
            emailPwd = prop.getProperty("emailPwd");
            emailHost = prop.getProperty("emailHost");
            emailAuth =prop.getProperty("emailAuth");
            emailProtocol = prop.getProperty("emailProtocol");
            emailPort = Integer.parseInt(prop.getProperty("emailPort"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     *
     * @param to  		收件人邮箱账号
     * @param emailMsg  邮件内容
     * @return
     */
    public static boolean sendMail(String to, String emailMsg) {
        try {
            //获取系统环境信息
            Properties props = System.getProperties();
            //设置邮件服务器
            props.setProperty("mail.smtp.host", emailHost);
            //设置密码认证
            props.setProperty("mail.smtp.auth", emailAuth);
            //设置传输协议
            props.setProperty("mail.transport.protocol", emailProtocol);
            //创建session对象
            Session session = Session.getInstance(props);
            //设置输出日志
            session.setDebug(true);

            //邮件发送对象
            MimeMessage message = new MimeMessage(session);
            //设置发件人
            message.setFrom(new InternetAddress(emailUser));
            //设置邮件主题
            message.setSubject("come on,baby");
            //设置邮件内容
            //message.setText("Welcome to JavaMail World!");
            //如果带网页内容使用Content发送
            message.setContent((emailMsg),"text/html;charset=utf-8");

            //获取邮件发送管道
            Transport transport=session.getTransport();
            //连接管道
            transport.connect(emailHost,emailPort, emailUser, emailPwd);
            //发送邮件
            transport.sendMessage(message,new Address[]{new InternetAddress(to)});
            //关闭管道
            transport.close();
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}
